package cn.school.controller.student;

import cn.school.classManage.dao.SchoolClassDao;
import cn.school.classManage.vo.SchoolClassVO;
import cn.school.common.api.CommonPage;
import cn.school.common.api.CommonResult;
import cn.school.common.utils.SysUserUtils;
import cn.school.student.dao.StudentInfoDao;
import cn.school.student.entity.ExamPublish;
import cn.school.student.entity.TExamPerformance;
import cn.school.student.entity.vo.ExamPublishParam;
import cn.school.student.entity.vo.ExamPublishVo;
import cn.school.student.entity.vo.ExamTestPassRatioVo;
import cn.school.student.entity.vo.StudentInfoVo;
import cn.school.student.service.ExamPublishService;
import cn.school.sys.entity.SysUser;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/Publish")
public class TExamPublishController {

    @Autowired
    private ExamPublishService examPublishService;

    @Autowired
    private StudentInfoDao studentInfodao;




    @PostMapping("/insert")
    @ApiOperation("insert => 发布试卷")
    public CommonResult<Boolean> insert(@RequestBody ExamPublish examPublish) {
        SysUser sysUser = SysUserUtils.getSysUser();
        examPublish.setCreateBy(sysUser.getUsername());
        examPublish.setCreateTime(new Date());
        int insert = examPublishService.insert(examPublish);
        if (insert != 0) {
            return CommonResult.success();
        }
        return CommonResult.failed();
    }

    @PostMapping("query")
    @ApiOperation("query=>查询试卷")
    public CommonResult<CommonPage> query(@RequestBody ExamPublishParam examPublishParam) {
        CommonPage<ExamPublishVo> examPublishVOCommonPage = examPublishService.queryList(examPublishParam);
        return CommonResult.success(examPublishVOCommonPage);
    }

    @PostMapping("/queryByStudnet")
    public CommonResult<CommonPage> queryByStudnet(@RequestBody ExamPublishParam examPublishParam) {
        CommonPage<StudentInfoVo> page = new CommonPage<>();
        Integer pageNo = (examPublishParam.getPageNum()-1)*examPublishParam.getPageSize();
        examPublishParam.setPageNum(pageNo);
        List<StudentInfoVo> list = this.studentInfodao.queryList(examPublishParam);
        page.setPageNum(pageNo);
        page.setPageSize(examPublishParam.getPageSize());
        page.setTotal(this.studentInfodao.count(examPublishParam));
        page.setList(list);

        return CommonResult.success(page);
    }

    @PostMapping("/studentquery")
    public CommonResult<List<ExamPublishVo>> studentquery(@RequestBody Map<String,String> paramMap) {

        String studentId = paramMap.get("studentId");
        String publishId = paramMap.get("publishId");
        return CommonResult.success(this.examPublishService.selectByquesitonsName(studentId, publishId));

    }

    /**
     * 撤销
     * @param publishId
     * @return
     */
    @GetMapping("/cancelPublish/{publishId}")
    public CommonResult<String> cancelPublish(@PathVariable("publishId") String publishId) {

        int num = examPublishService.cancelPublish(publishId);
        if(num>0){
            return CommonResult.success();
        }
        return CommonResult.failed();

    }

    /**
     * 批改学生作业
     * @param tExamPerformance
     * @return
     */
    @PostMapping("/correctExamByStudent")
    public CommonResult<String> correctExamByStudent(@RequestBody List<TExamPerformance> tExamPerformance) {
        if(tExamPerformance.isEmpty()){
            return CommonResult.failed("数据异常");
        }
        int num = examPublishService.correctExamByStudent(tExamPerformance);
        if(num>0){
            return CommonResult.success();
        }
        return CommonResult.failed();

    }

    // 查询试卷每道题目的学生的通过率
    @GetMapping("/getQuestionPassRatioByPublishId/{publishId}")
    @ApiOperation("getQuestionPassRatioByPublishId => 获取每道题目的学生通过率")
    public CommonResult<List<ExamTestPassRatioVo>> getQuestionPassRatio(@PathVariable("publishId")Long publishId) {
        List<ExamTestPassRatioVo> result = examPublishService.getQuestionPassRatioByPublishPaper(publishId);
        return CommonResult.success(result);
    }

}
